faApp.controller('roadVideoAddCtrl',['$scope','$location','$controller','$route','$filter','$routeParams','roadVideoService','userService','productService','simuService',function($scope,$location,$controller,$route,$filter,$routeParams,roadVideoService,userService,productService,simuService) {
  $controller('BaseCtrl', {$scope: $scope});

  $scope.id = $routeParams.id;

  $scope.item = {'isEncryption':1};
  $scope.player = '';

  if($scope.id){
    roadVideoService.detailById($scope.id).then(function(json){
      if (json.statusCode){
        alert(json.statusCode);
      }else{
        $scope.item = json.dto;
      }
    });
  };

  var tmpType = '';
  $scope.$watch('item.productType', function() {
    if($scope.item.productType && ($scope.item.productType == '04')){
      alert('暂不支持此类型！');
      $scope.item.productType = tmpType;
      tmpType = '';
      return;
    }

    if($scope.item.productType && (tmpType != $scope.item.productType)){
      $scope.loading = true;
      tmpType = $scope.item.productType;
      var param = {
        pageNo:1, 
        pageSize:1000,
        pTypeList: $scope.item.productType, 
        pPublicStatus:'2',
        activitiesCode:'lysp',
        videoId : $scope.item.id || ''
      };
      if($scope.item.productType == '01'){
        param.pubStatus = 1;
      };
      // 根据类型查询对应的产品列表
      if($scope.item.productType == '06'){
        // PE/VC
        
        productService.queryProductList(param).then(function(json){
          $scope.productList = json.list;

          // 删除已经添加的！
          // exclude();

          $scope.loading = false;
        });
      }else if($scope.item.productType == '02' || $scope.item.productType == '03'){
        // 信托资管
        productService.productList(param).then(function(json){
          $scope.productList = json.projectList;
          
          // 删除已经添加的！
          // exclude();
          
          $scope.loading = false;
        });
      }else if($scope.item.productType == '01'){
        // 阳光私募
        simuService.list(param).then(function(json){
          $scope.productList = json.list;
          
          // 删除已经添加的！
          // exclude();

          $scope.loading = false;
        });
      }
    }
  }, true);

  $scope.onUploadFile({
    showMessage: function(m) {
      alert(m);
    },
    onComplete: function(f, response, id) {
      console.log(id);
      id--;
      if (response.statusCode == "0000") {
        if (id == 0) {
          $scope.item.imagePath = App.staticHost + response.attribute.fileUrl;
          $scope.$apply();
        } else if (id == 1) {
          $scope.item.imagePathForProduct = App.staticHost + response.attribute.fileUrl;

          $scope.$apply();
        } else if(id == 2) {
          $scope.item.shareImage = App.staticHost + response.attribute.fileUrl;
          $scope.$apply();
        }else {
          alert('上传失败');
        }
      }
    }
  });

  $scope.save = function() {
    if ($scope.form.$invalid) {
      alert('填写必填项');
    }else if($scope.item.title.length>20){
        alert('视频标题最多输入20个字符');
    }else{
      if($scope.id){
        roadVideoService.edit($scope.item).then(function(json){
          if(json.statusCode){
            alert(json.statusCode)
          }else{
            alert('修改成功');
            $route.reload()
          }
        });
      }else{
        if($scope.item.productType == '01'){
          $scope.item.pubStatus = 1;
        };
        roadVideoService.add($scope.item).then(function(json){
          if(json.statusCode){
            alert(json.statusCode)
          }else{
            $location.path('/roadVideo/list');
          }
        });
      };
    }
  };

  $scope.products = [];
  $scope.searchProduct = function() {
    $scope.showSearchCombox = true;
    $scope.loaded = false;
    // productService.productList({pPublicStatus:2,pName:$scope.productKw,pSaleStatus:'03'}).then(function(json){
    productService.productList({
      pPublicStatus: 2,
      pName: $scope.productKw
    }).then(function(json) {
      $scope.products = json.projectList;
      $scope.pager = json.projectDTO;
      $scope.loaded = true;
    });
  };

  $scope.selectProduct = function(id) {
    $scope.item.pId = id;

    angular.forEach($scope.products, function(o) {
      if (o.id == id) {

        millionService.list({
          pId: id,
          activitiesCode: $scope.item.activitiesCode
        }).then(function(json) {
          if (json && json.list && json.list.length > 0) {
            alert("此产品已经在参与活动了");
            $scope.productKw = "";
            return;
          } else {
            $scope.product = o;
            $scope.productKw = o.pName;

            productService.productQuery($scope.product.id).then(function(json) {
              $scope.product = json.project;
              $scope.loading = false;
              $scope.projectRabateList = json.projectRabateList;
              $scope.minRebate = 99999999;
              if ($scope.projectRabateList && $scope.projectRabateList.length > 0) {
                angular.forEach($scope.projectRabateList, function(o) {
                  if (o.pRabateProfit < $scope.minRebate) {
                    $scope.minRebate = o.pRabateProfit;
                  }
                });
              }
            });
          }
        });

        $scope.showSearchCombox = false;
      }
    });
  };

  $scope.playVideo = function(v){
    if(!v){
      alert('输入视频ID');
      return;
    }else{
      $scope.showPlayBox = true;
      $scope._videoId = v;
      $scope.player = new YKU.Player('youkuplayer',{
        styleid: '0',
        client_id: YK_CLIENT_ID,
        vid: $scope._videoId,
        newPlayer: true
      })
    }
  }
  $scope.stopPlay = function(){
    $scope._videoId = '';
    $scope.showPlayBox = false;
  }
  
}]);

faApp.controller('roadVideoListCtrl',['$scope','$location','$controller','$route','roadVideoService',function($scope,$location,$controller,$route,roadVideoService) {
  $controller('BaseListCtrl', {$scope: $scope}); 
  $scope.loading = true;

  $scope.config = {
    "pageSize" : 20,
    "pageNo" : 1
  };
  $scope.newsList = {};
  $scope.$watch('config',function(){
    roadVideoService.list($scope.config).then(function(json){
      $scope.list = json.list || '';
      $scope.pager = json.pager;
      for( i in $scope.list){
        if($scope.list[i].isEncryption == 1){
          $scope.encryptionPassword = $scope.list[i].encryptionPassword;
          return;
        }else{
          $scope.encryptionPassword = '';
        }
      }

    });
  },true);

  $scope.search = function() {
    angular.forEach($scope.searchMap, function(o, i) {
      $scope.config[i] = (undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });
    $scope.config.pageNo = 1;
  };

  $scope.delete = function(id){
    if(confirm('确定删除？')){
      roadVideoService.delete(id).then(function(json) {
        if (json.statusCode) {
          alert(json.statusCode)
        } else {
          $route.reload();
        }
      })
    }

  };

  $scope.orderby = function(v){
    if(v == 'createDate'){
      $scope.config.orderParameter = '01';
    }else if(v == 'updateDate'){
      $scope.config.orderParameter = '02';
    }else if(v == 'playNum'){
      $scope.config.orderParameter = '03';
    };
    if($scope.config.orderType == '01'){
      $scope.config.orderType = '02';
    }else{
      $scope.config.orderType = '01';
    }

  };

  $scope.saveEncryptionPassword = function(){
    if($scope.encryptionPassword){
      roadVideoService.videoPassword($scope.encryptionPassword).then(function(json){
        if(json.statusCode){
          alert('json.statusCode');
        }else{
          alert('密码修改成功');
        }
      })
    }
  }

}]);

faApp.controller('roadVideoPlayCtrl',['$scope','$location','$controller','$route','roadVideoService',function($scope,$location,$controller,$route,roadVideoService) {
  $controller('BaseListCtrl', {$scope: $scope});
  $scope.id = $routeParams.id;
  player = new YKU.Player('youkuplayer',{
    styleid: '0',
    client_id: 'YOUR YOUKUOPENAPI CLIENT_ID',
    vid: '替换成优酷视频ID',
    newPlayer: true
  });


}]);